package com.scalar.chiptrack.commons.dao;

import com.scalar.chiptrack.commons.bean.IResultMetaData;
import com.scalar.chiptrack.commons.bean.IResultMetaDataImpl;
import com.scalar.chiptrack.db.DataSource;

import java.util.ArrayList;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;

/**
  * User: Ganesh
  * Date: Apr 29, 2008
  * Time: 11:25:58 AM
 */

public class ResultMetaDataDAO
{
    public static ArrayList loadMetaData(String tableName)
    {
          return loadMetaData(tableName,true);
    }
    public static ArrayList loadMetaData(String tableName,boolean sortByOrder)
    {
          Connection conn=null;
          ResultSet rs=null;
          PreparedStatement ps=null;
          ArrayList columnMetaDataList=new ArrayList();
          try
          {
              conn= DataSource.getConnection();
              ps=conn.prepareStatement("SELECT ROW_ID,TABLE_NAME,COLUMN_NAME,DIAPLAY_NAME,DISPLAY,COLUMN_WIDTH,SORT_ORDER,COUMPULSORY,ALIGN FROM META_DATA WHERE TABLE_NAME=? ORDER BY SORT_ORDER");
              ps.setString(1,tableName);
              rs=ps.executeQuery();
              int i=1;
              while(rs.next())
              {
                  IResultMetaData resultMetaData=new IResultMetaDataImpl();
                  resultMetaData.setRowId(i);
                  resultMetaData.setTableName(rs.getString(2));
                  resultMetaData.setColumnName(rs.getString(3));
                  resultMetaData.setDisplayName(rs.getString(4));
                  resultMetaData.setDisplay(rs.getBoolean(5));
                  resultMetaData.setColWidth(rs.getInt(6));
                  resultMetaData.setSortOrder(rs.getString(7));
                  resultMetaData.setCompulsory(rs.getBoolean(8));
                  resultMetaData.setAlign(rs.getString(9));
                  columnMetaDataList.add(resultMetaData);
                  i++;
              }
          }
          catch(Exception e)
          {
              System.out.println("Exception in Result Meta Data DAO");
              e.printStackTrace();
          }
          finally
          {
              try
              {
                  if(rs!=null){rs.close();}
                  if(ps!=null){ps.close();}
                  if(conn!=null){conn.close();}
              }
              catch( Exception e )
              {
                  e.printStackTrace();
              }
          }
      return columnMetaDataList;
   }
}
